/**
 * 
 */
package com.bms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.bms.Logic;
import com.bms.abstracts.RESTController;
import com.bms.domain.Groups;
import com.bms.domain.User;

/**
 * @author Administrator
 *
 */
public class UserListController extends RESTController {
	
	private Logic logic;
	
	public void setLogic(Logic logic) {
		this.logic = logic;
	}

	/* (non-Javadoc)
	 * @see com.bms.abstracts.RESTController#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView doGet(HttpServletRequest request,
			HttpServletResponse response) {
		List<User> list = logic.getUserList();
		for(User u:list) log.debug("--------------->"+u.getUsername());
		
		List<Groups> groups = logic.getGroupList();
		request.setAttribute("groups", groups);
		return new ModelAndView("userList","list",list);
	}

	/* (non-Javadoc)
	 * @see com.bms.abstracts.RESTController#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView doPost(HttpServletRequest request,
			HttpServletResponse response) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String nickname = request.getParameter("nickname");
		String groups = request.getParameter("groups");
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setNickname(nickname);
		Groups g = logic.getGroup(groups);
		user.setGroups(g);
		user.setStatus(1);
		logic.addUser(user);
		return doGet(request, response);
	}

}
